using System;
using System.ComponentModel;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Web.UI;
using System.Web;
using System.Web.UI.WebControls;
using BlogEngine.Core;
using System.Text;

namespace Controls
{
    public abstract class PhotoFlashBase :WebControl
    {
        private Dictionary<string,string> _Params = new Dictionary<string,string>();
        private Dictionary<string,string> _Variables = new Dictionary<string,string>();
        private const string _PARAM = "so.addParam(\"{0}\",\"{1}\");\n";
        private const string _VARIABLE = "so.addVariable(\"{0}\",\"{1}\");\n";

		internal string _mediaPlayerLocation = @"~/mediaplayer/";

        protected PhotoFlashBase() :base()
        {
        }

        private string MediaPlayerPath
        {
			get { return Utils.ConvertToAbsolute(this.ResolveUrl(_mediaPlayerLocation + MediaPlayer)).ToString(); }
        }

        private string MediaPlayerLocation
        {
			get { return System.Web.HttpContext.Current.Server.MapPath(_mediaPlayerLocation) + MediaPlayer; }
        }

        private string _MediaPlayer = string.Empty;

        protected string MediaPlayer
        {
            get { return _MediaPlayer; }
            set { _MediaPlayer = value; }
        }

        protected void AddParam(string key, string value)
        {
            _Params.Add(key, value);
        }

        protected void AddVariable(string key, string value)
        {
            _Variables.Add(key, value);
        }

        protected override void OnPreRender(EventArgs e)
        {
            Utils.RequireStandardJavaScript("swfObject");
            base.OnPreRender(e);
        }

        protected void RenderScript(HtmlTextWriter writer, string id)
        {
            string result;

            //see if we have the software available to display the flash object
            if (!PlayerAvailable())
            {
                result = "<p>The objects needed to play this content are missing (eg. swfobject.js) or your settings are incorrect</p>";
            }
            else
            {
                Utils.RequireStandardJavaScript("swfObject");

                StringBuilder sb = new StringBuilder();
                string myId = string.Format("{0}_{1}", id, Guid.NewGuid().ToString());

                sb.AppendLine("<div id=\"" + id + "\">");
                sb.AppendLine("<div id=\"" + myId + "\">");
                sb.AppendLine("<p>The <a href=\"http://www.macromedia.com/go/getflashplayer\">Flash Player</a> is required to show this content.</p>");
                sb.AppendLine("</div>");
                sb.AppendLine("<script type=\"text/javascript\" defer=\"defer\">");
                sb.AppendLine("if (typeof SWFObject != \"undefined\") {");
                sb.AppendLine("var so = new SWFObject(\"" + MediaPlayerPath + "\", \"" + myId + "\", \"" + this.Width.Value.ToString() + "\", \"" + this.Height.Value.ToString() + "\", \"7\", \"#000000\")");

                //write the parameters
                foreach (string key in _Params.Keys)
                {
                    sb.AppendFormat(_PARAM, key, _Params[key]);
                }

                //write the variables
                foreach (string key in _Variables.Keys)
                {
                    sb.AppendFormat(_VARIABLE, key, _Variables[key]);
                }

                //write the activation 
                sb.AppendLine("so.write(\"" + myId + "\");");
                sb.AppendLine("}");
                sb.AppendLine("</script>");
                sb.AppendLine("</div>");

                result = sb.ToString();
            }

            writer.Write(result);
        }

        private bool PlayerAvailable()
        {
            if (!System.IO.File.Exists(MediaPlayerLocation))
                return false;

            return true;
        }

    }
}
